package dev.loong.boot3.repository.impl;

import dev.loong.boot3.pojo.User;
import dev.loong.boot3.repository.DocUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DocUserRepositoryImpl {
    private MongoTemplate mongoTemplate;
    @Autowired
    public DocUserRepositoryImpl(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    public List<User> findByUserNameLike(String userName) {

        return List.of();
    }


    public User find(Long id, String userName) {
        return null;
    }


    public User findUserByIdOrUserName(Long id, String userName) {
        var creiteriaId = Criteria.where("id").is(id);
        var creiteriaUserName = Criteria.where("userName").is(userName);
        var creiteria = new Criteria().orOperator(creiteriaId, creiteriaUserName);
        return this.mongoTemplate.findOne(Query.query(creiteria), User.class);
    }
}
